package com.ms.p2p.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.ms.common.exception.NoticeException;
import com.ms.p2p.dao.SettingDao;
import com.ms.p2p.entity.system.Setting;
import com.ms.p2p.utils.HttpRequstUtil;

import com.ms.p2p.utils.HttpTookit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;


@Service
public class IdCardQueryServiceImpl {

    @Autowired
    SettingDao settingDao;


    public boolean idCardQuery(String idCard, String realName) throws NoticeException,UnsupportedEncodingException {

        Setting setting = settingDao.findByCode("app_key");

        String  url = "http://op.juhe.cn/idcard/query";
        HashMap map = new HashMap();
        map.put("idcard",idCard);
        map.put("realname",realName);
        map.put("key",setting.getText());

            String response = HttpTookit.doPost(url,map);
            JSONObject jsonObject =  JSONObject.parseObject(response);
            if ("0".equals(jsonObject.getString("error_code"))) {
                int res = jsonObject.getJSONObject("result").getInteger("res");
                if( 1 == res ){
                   return true;
                }else if (2 == res){
                   return  false;
                }
            }else {
                throw new NoticeException( jsonObject.getString("reason"));

            }

            return true;
    }


}
